package com.digital.smartgrow.service.businesscenter.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.smartgrow.dao.businesscenter.DynamicFormDataMapper;
import com.digital.smartgrow.dto.businesscenter.DynamicFormDataPageDTO;
import com.digital.smartgrow.entity.businesscenter.DynamicFormData;
import com.digital.smartgrow.entity.datacenter.Teacher;
import com.digital.smartgrow.service.businesscenter.DynamicFormDataService;
import com.digital.smartgrow.service.datacenter.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 动态表单数据 服务实现类
 * </p>
 *
 * @author xiewb
 * @since 2022-04-23
 */
@Service
public class DynamicFormDataServiceImpl extends ServiceImpl<DynamicFormDataMapper, DynamicFormData> implements DynamicFormDataService {

    @Autowired
    private TeacherService teacherService;

    @Override
    public Object getListByPage(DynamicFormDataPageDTO pageDTO) {
        if (pageDTO == null) pageDTO = new DynamicFormDataPageDTO();
        Page<DynamicFormData> page = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        return this.baseMapper.getListByPage(page, pageDTO);
    }

    @Override
    public Object getInfo(Integer id) {
        DynamicFormData dynamicFormData = this.getById(id);
        if (dynamicFormData != null && dynamicFormData.getCreateId() != null) {
            Teacher teacher = teacherService.getById(dynamicFormData.getCreateId());
            if (teacher != null) {
                dynamicFormData.setName(teacher.getName());
                dynamicFormData.setEmployeeId(teacher.getEmployeeId());
            }
        }
        return dynamicFormData;
    }

    @Override
    public Object getFormCount(Integer formTemplateId) {
        long total = teacherService.lambdaQuery().count();
        long realTotal = this.lambdaQuery().eq(DynamicFormData::getFormTemplateId, formTemplateId).count();
        Map result = new HashMap();
        result.put("total", total);
        result.put("realTotal", realTotal);
        return result;
    }
}
